package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db1.PcbaLineDao;
import com.sailfish.springbootdemo.dao.db1.PcbaProcessDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.PcbaLine;
import com.sailfish.springbootdemo.pojo.db1.PcbaProcess;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PcbaProcessServiceImpl implements PcbaProcessService {

    @Autowired
    private PcbaProcessDao pcbaProcessDao;

    @Autowired
    private CommonJudgeCanDeleteService commonService;

    @Autowired
    private PcbaLineDao pcbaLineDao;

    @Autowired
    private OperateService operateService;

    private String operateSmallType = "工序管理";
    private String operateSmallType_2 = "修改工序关联修改线体名";

    @Override
    public List<PcbaProcess> getAllPcbaProcess(String searchValue) {
        List<PcbaProcess> list = pcbaProcessDao.getAllPcbaProcess(searchValue);
        return list;
    }

    @Override
    public Result deletePcbaProcessById(Integer pcbaProcessId) {
        try {
            if (pcbaProcessId == null || "".equals(pcbaProcessId.toString())) {
                return ResultUtil.error(500, "缺少参数pcbaProcessId", "Miss Param pcbaProcessId", null);
            }
            Map<String, Integer> stringIntegerMap = commonService.judgeCanDeletePcbaProcess(pcbaProcessId);
            if (stringIntegerMap.get("pcbaLineNum") > 0) {
                return ResultUtil.error(500,
                        "禁止删除，当前项" + stringIntegerMap.get("pcbaLineNum") + "处关联线体",
                        "Forbidden Delete", null);
            }

            String userId = UserHeaderHolder.getUserId();
            PcbaProcess oldPcbaProcess = pcbaProcessDao.getPcbaProcessById(pcbaProcessId);

            Integer integer = pcbaProcessDao.deletePcbaProcessById(pcbaProcessId);
            if (integer > 0) {

                // 操作日志
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 2, 1, new Date(), JSONObject.toJSONString(oldPcbaProcess), null));

                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public PcbaProcess getPcbaProcessById(Integer pcbaProcessId) {
        return pcbaProcessDao.getPcbaProcessById(pcbaProcessId);
    }

    @Override
    public Result addOrUpdatePcbaProcess(String mode, PcbaProcess pcbaProcess) {
        try {
            if (pcbaProcess == null) {
                return ResultUtil.error(500, "缺少参数pcbaProcess", "Miss Param pcbaProcess", null);
            }
            if (mode == null || "".equals(mode)) {
                return ResultUtil.error(500, "缺少参数mode", "Miss Param mode", null);
            }
            if (!("edit".equals(mode) || "add".equals(mode))) {
                return ResultUtil.error(500, "mode 参数错误", "mode Param Error", null);
            }

            String userId = UserHeaderHolder.getUserId();

            if ("edit".equals(mode)) {
                if (pcbaProcess.getPcbaProcessId() == null || "".equals(pcbaProcess.getPcbaProcessId().toString())) {
                    return ResultUtil.error(500, "缺少参数pcbaProcessId", "Miss Param pcbaProcessId", null);
                }

                List<PcbaProcess> pcbaProcesses = pcbaProcessDao.judgeUniquePcbaProcess(pcbaProcess.getPcbaProcessId(), pcbaProcess.getPcbaProcessNameCn());
                if (pcbaProcesses.size() > 0) {
                    return ResultUtil.error(500, "修改失败，存在相同名字", "Operate Fail", null);
                }

                PcbaProcess oldPcbaProcess = pcbaProcessDao.getPcbaProcessById(pcbaProcess.getPcbaProcessId());

                Date d = new Date();
                pcbaProcess.setUpdateTime(d);
                Integer integer = pcbaProcessDao.updatePcbaProcess(pcbaProcess);
                if (integer > 0) {

                    // 需要将 相关线体名 按照新的工序重新命名
                    List<PcbaLine> oldPcbaLineList = new ArrayList<>();
                    List<PcbaLine> newPcbaLineList = new ArrayList<>();

                    String oldPcbaProcessNameCn = oldPcbaProcess.getPcbaProcessNameCn();
                    String newPcbaProcessNameCn = pcbaProcess.getPcbaProcessNameCn();
                    List<PcbaLine> allPcbaLine = pcbaLineDao.getAllPcbaLine(null, null, null, pcbaProcess.getPcbaProcessId(), null, null, null, null, null, null, null);
                    for (PcbaLine pcbaLine : allPcbaLine) {
                        oldPcbaLineList.add(pcbaLine);

                        Integer pcbaLineId = pcbaLine.getPcbaLineId();
                        PcbaLine line = pcbaLineDao.getPcbaLineById(pcbaLineId);
                        String pcbaLineNameCn = line.getPcbaLineNameCn();
                        pcbaLineNameCn = pcbaLineNameCn.replace(oldPcbaProcessNameCn, newPcbaProcessNameCn);

                        PcbaLine updatePcbaLine = new PcbaLine();
                        updatePcbaLine.setPcbaLineId(pcbaLineId);
                        updatePcbaLine.setUpdateTime(d);
                        updatePcbaLine.setPcbaLineNameCn(pcbaLineNameCn);
                        updatePcbaLine.setPcbaLineNameEn(pcbaLineNameCn);

                        pcbaLineDao.updatePcbaLine(updatePcbaLine);
                        newPcbaLineList.add(updatePcbaLine);
                    }

                    // 操作日志
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType_2, 3, 3, d, JSONObject.toJSONString(oldPcbaLineList), JSONObject.toJSONString(newPcbaLineList)));
                    // 操作日志
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, d, JSONObject.toJSONString(oldPcbaProcess), JSONObject.toJSONString(pcbaProcess)));

                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            } else {

                List<PcbaProcess> pcbaProcessByName = pcbaProcessDao.getPcbaProcessByName(pcbaProcess.getPcbaProcessNameCn());
                if (pcbaProcessByName.size() > 0) {
                    return ResultUtil.error(500, "该工序已存在", "Already Exist", null);
                }

                pcbaProcess.setCreateTime(new Date());
                Integer integer = pcbaProcessDao.addPcbaProcess(pcbaProcess);
                if (integer > 0) {

                    // 操作日志
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 1, 1, new Date(), null, JSONObject.toJSONString(pcbaProcess)));

                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
